using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;

using DQS.EL;

namespace DQS.DAL
{
    public static class DQ_ServiceIDService
    {
        public static void InsertServiceIDs(IList<DQ_SERVICEID> argSIDs)
        {
            String idStr = GetIdString(argSIDs);
            using (OracleConnection oc = new OracleConnection(OracleHelper.connString))
            {
                oc.Open();
                OracleTransaction ot = oc.BeginTransaction();
                OracleParameter[] _paramClob = {
                    new OracleParameter("tmp_clob", OracleType.Clob)
                };
                _paramClob[0].Direction = ParameterDirection.Output;
                OracleHelper.ExecuteNonQuery(ot, "DQ_Creat_TempClob_P", CommandType.StoredProcedure, _paramClob);
                OracleLob lob = OracleLobHandler.ToByte(_paramClob[0].Value, idStr);
                OracleParameter[] _params = { 
                    new OracleParameter("query_id",OracleType.Number),
                    new OracleParameter("sids",OracleType.Clob)
                };
                _params[0].Value = argSIDs[0].QUERY_ID;
                _params[1].Value = lob;
                OracleHelper.ExecuteNonQuery(ot, "DQ_Insert_ServiceID_P", CommandType.StoredProcedure, _params);
                ot.Commit();
            }
        }

        private static String GetIdString(IList<DQ_SERVICEID> argSIDs)
        {
            StringBuilder sb = new StringBuilder();            
            foreach (DQ_SERVICEID sid in argSIDs)
            {                
                sb.Append(sid.SERVICE_ID);
                sb.Append("|");
            }
            sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
        }
    }
}
